package net.citrite.android.render;

import com.android.ide.common.rendering.LayoutLibrary;
import com.android.ide.common.rendering.api.ViewInfo;
import net.citrite.android.Severity;

import java.lang.reflect.Method;


public class Button extends Control {
    private int maxHeight;
    private int maxWidth;
    private int layoutWidth;
    private int layoutHeight;
    private String text;
    private int textHeight;
    private int textWidth;
    private int padLeft;
    private int padRight;
    private int padTop;
    private int padBottom;
    private float textSize;
    public Button(ViewInfo info, LayoutLibrary layoutLibrary) throws Exception {
        super(info, layoutLibrary);
        control = layoutLibrary.getClassLoader().loadClass("android.widget.Button");

        Method getPaintMethod = control.getMethod("getPaint");
        Method getTextMethod = control.getMethod("getText");
        Method getTextSizeMethod = control.getMethod("getTextSize");
        Method getMaxWidthMethod = control.getMethod("getMaxWidth");
        Method getMaxHeightMethod = control.getMethod("getMaxHeight");
        //Method getTypefaceMethod = control.getMethod("getTypeface");
        Method getCompoundPadLeftMethod = control.getMethod("getCompoundPaddingLeft");
        Method getCompoundPadRightMethod = control.getMethod("getCompoundPaddingRight");
        Method getCompoundPadTopMethod = control.getMethod("getCompoundPaddingTop");
        Method getCompoundPadBottomMethod = control.getMethod("getCompoundPaddingBottom");
        Object paintObj = getPaintMethod.invoke(info.getViewObject());
        //Object fontObj = getTypefaceMethod.invoke(info.getViewObject());

        Class textPaint = layoutLibrary.getClassLoader().loadClass("android.text.TextPaint");
        Class rect = layoutLibrary.getClassLoader().loadClass("android.graphics.Rect");
        //Class font = layoutLibrary.getClassLoader().loadClass("android.graphics.Typeface");

        Object rectObj = rect.newInstance();
        //Method setTypefaceMethod = textPaint.getMethod("setTypeface", font);
        //Method setTextSizeMethod = textPaint.getMethod("setTextSize", float.class);
        Method getTextBoundsMethod = textPaint.getMethod("getTextBounds", String.class, int.class, int.class, rect);

        text = (String) getTextMethod.invoke(info.getViewObject());
        textSize = (float)getTextSizeMethod.invoke(info.getViewObject());

        //setTypefaceMethod.invoke(paintObj, fontObj);
        //setTextSizeMethod.invoke(paintObj, textSize);
        getTextBoundsMethod.invoke(paintObj, text, 0, text.length(), rectObj);
        Method rectHeight = rect.getMethod("height");
        Method rectWidth = rect.getMethod("width");

        textHeight = (int)rectHeight.invoke(rectObj);
        textWidth = (int)rectWidth.invoke(rectObj);
        if (!text.isEmpty() && textSize > 0)
            textHeight = (int)textSize;
        maxHeight = (int)getMaxHeightMethod.invoke(info.getViewObject());
        maxWidth = (int)getMaxWidthMethod.invoke(info.getViewObject());
        padLeft = (int)getCompoundPadLeftMethod.invoke(info.getViewObject());
        padTop = (int)getCompoundPadTopMethod.invoke((info.getViewObject()));
        padRight = (int)getCompoundPadRightMethod.invoke(info.getViewObject());
        padBottom = (int)getCompoundPadBottomMethod.invoke(info.getViewObject());
        layoutWidth = info.getRight() - info.getLeft();
        layoutHeight = info.getBottom() - info.getTop();
    }

    @Override
    public Severity measure() {
        int preferWidth = layoutWidth - padLeft - padRight;
        int preferHeight = layoutHeight - padBottom - padTop;
        int wrap = (preferWidth != 0) ? textWidth/preferWidth : 0;
        while (wrap > 1) {
            preferHeight -= textHeight;
            wrap --;
        }
        if (preferHeight < textHeight) {
            return Severity.WARNING;
        }
        return Severity.None;
    }
}
